{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "colab": {
      "name": "quick_start.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "# **在线体验**\n",
        "<a href=\"https://colab.research.google.com/github/AgentMaker/AgentOCR/blob/main/examples/quick_start.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>",
        "<p></p>",
        "<a href=\"https://aistudio.baidu.com/aistudio/projectdetail/2295574\" target=\"_parent\"><img src=\"https://img.shields.io/badge/AIStudio-Open%20In%20AIStudio-blue\" alt=\"Open In AIStudio\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": false,
        "id": "GJd_RCY83Qz8"
      },
      "source": [
        "# **安装**\n",
        "* AgentOCR 可直接通过 pip 进行下载安装\n",
        "* Package 依赖 ONNXRuntime，不过由于 ONNXRuntime 的版本过多，安装 AgentOCR 不会自动安装该依赖\n",
        "* 需要根据 ONNX 官网安装提示，手动选择合适的平台版本进行安装"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PoRRzwm-3Qz8"
      },
      "source": [
        "# 安装 AgentOCR\n",
        "!pip install agentocr\n",
        "\n",
        "# 安装 CPU 版本 ONNXRuntime\n",
        "!pip install onnxruntime\n",
        "\n",
        "# 安装 GPU 版本 ONNXRuntime\n",
        "# !pip install onnxruntime-gpu"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "b-IZF8FJ3dHc"
      },
      "source": [
        "# **下载测试图片**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y3UCWXFS3cX0"
      },
      "source": [
        "!mkdir images\n",
        "!wget https://ai-studio-static-online.cdn.bcebos.com/c4c758456cc74d9e997a72a25c47a3a57707b0664ed546819e4d5981ba765b13 -O images/ch.jpg\n",
        "!wget https://ai-studio-static-online.cdn.bcebos.com/48dd39f3671c44d9b58564811ebeab36bc931fa4f9e64ce4bfa9783706cc85bf -O images/en.jpg\n",
        "!wget https://ai-studio-static-online.cdn.bcebos.com/d4f9dd815d024114bd4eee7dca03f7222c135073b01642f3866684b404b75d2a -O images/korean.jpg "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": false,
        "id": "2Q7Ns0EW3Qz8"
      },
      "source": [
        "# **使用**\n",
        "* 支持多语言识别模型，可通过内置的配置文件进行快速切换"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": false,
        "id": "XkEhzkVb3Qz9"
      },
      "source": [
        "## **中/英文识别**\n",
        "![](https://ai-studio-static-online.cdn.bcebos.com/c4c758456cc74d9e997a72a25c47a3a57707b0664ed546819e4d5981ba765b13)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gHRN1CjY3Qz9",
        "outputId": "2c61437e-dd64-4e72-b5cc-a5e138938663"
      },
      "source": [
        "from agentocr import OCRSystem\n",
        "\n",
        "# 通过 config 参数来进行模型配置，内置多国语言的配置文件\n",
        "ocr = OCRSystem(config='ch')\n",
        "\n",
        "# 设置测试图片路径\n",
        "img_path = 'images/ch.jpg'\n",
        "\n",
        "# 调用 OCR API 进行全流程识别\n",
        "result = ocr.ocr(img_path)\n",
        "\n",
        "# 打印结果\n",
        "for line in result:\n",
        "    print(line)"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[2021/08/20 09:13:57] root INFO: All available providers: ['CPU']\n",
            "[2021/08/20 09:13:57] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:13:57] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:13:57] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:13:57] root DEBUG: dt_boxes num : 16, elapse : 0.11015558242797852\n",
            "[2021/08/20 09:13:57] root DEBUG: rec_res num  : 16, elapse : 0.4949636459350586\n",
            "[[[26.0, 37.0], [301.0, 39.0], [301.0, 72.0], [25.0, 70.0]], ['纯臻营养护发素', 0.97137797]]\n",
            "[[[27.0, 82.0], [172.0, 82.0], [172.0, 103.0], [27.0, 103.0]], ['产品信息/参数', 0.99220866]]\n",
            "[[[28.0, 113.0], [330.0, 113.0], [330.0, 133.0], [28.0, 133.0]], ['45元/每公斤，100公斤起订）', 0.9619098]]\n",
            "[[[25.0, 143.0], [283.0, 144.0], [283.0, 164.0], [25.0, 163.0]], ['每瓶22元，1000瓶起订）', 0.9908634]]\n",
            "[[[24.0, 177.0], [301.0, 176.0], [301.0, 195.0], [24.0, 196.0]], ['【品牌】：代加工方式/OEMODM', 0.98346674]]\n",
            "[[[26.0, 210.0], [232.0, 210.0], [232.0, 227.0], [26.0, 227.0]], ['【品名】：纯臻营养护发素', 0.9786152]]\n",
            "[[[24.0, 239.0], [241.0, 237.0], [241.0, 257.0], [25.0, 259.0]], ['【产品编号】：YM-X-3011', 0.9821852]]\n",
            "[[[415.0, 241.0], [429.0, 241.0], [429.0, 300.0], [415.0, 300.0]], ['DMOEM', 0.88782585]]\n",
            "[[[25.0, 272.0], [180.0, 270.0], [180.0, 288.0], [25.0, 289.0]], ['【净含量】：220ml', 0.9954416]]\n",
            "[[[26.0, 304.0], [251.0, 304.0], [251.0, 320.0], [26.0, 320.0]], ['【适用人群】：适合所有肤质', 0.9606327]]\n",
            "[[[25.0, 334.0], [342.0, 335.0], [342.0, 352.0], [25.0, 351.0]], ['【主要成分】：鲸蜡硬脂醇、燕麦-葡聚', 0.97208077]]\n",
            "[[[27.0, 367.0], [279.0, 367.0], [279.0, 381.0], [27.0, 381.0]], ['糖、椰油酰胺内基甜菜碱、泛醇', 0.854405]]\n",
            "[[[368.0, 370.0], [474.0, 370.0], [474.0, 384.0], [368.0, 384.0]], ['（成品包材）', 0.9908913]]\n",
            "[[[27.0, 398.0], [360.0, 398.0], [360.0, 412.0], [27.0, 412.0]], ['（主要功能】：可紧致头发磷层，从而达到', 0.9463476]]\n",
            "[[[28.0, 431.0], [369.0, 431.0], [369.0, 444.0], [28.0, 444.0]], ['即时持久改善头发光泽的效果，给十燥的头', 0.9470975]]\n",
            "[[[28.0, 460.0], [135.0, 460.0], [135.0, 477.0], [28.0, 477.0]], ['发足够的滋养', 0.9977181]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": false,
        "id": "0tD9vKs93Qz9"
      },
      "source": [
        "## **英文识别**\n",
        "![](https://ai-studio-static-online.cdn.bcebos.com/48dd39f3671c44d9b58564811ebeab36bc931fa4f9e64ce4bfa9783706cc85bf)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5BrdG2_r3Qz-",
        "outputId": "88c2f3d4-32ae-4b8a-8d17-dad41a19fa1d"
      },
      "source": [
        "from agentocr import OCRSystem\n",
        "\n",
        "# 通过 config 参数来进行模型配置，内置多国语言的配置文件\n",
        "ocr = OCRSystem(config='en')\n",
        "\n",
        "# 设置测试图片路径\n",
        "img_path = 'images/en.jpg'\n",
        "\n",
        "# 调用 OCR API 进行全流程识别\n",
        "result = ocr.ocr(img_path)\n",
        "\n",
        "# 打印结果\n",
        "for line in result:\n",
        "    print(line)"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[2021/08/20 09:14:01] root INFO: All available providers: ['CPU']\n",
            "[2021/08/20 09:14:01] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:14:01] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:14:01] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:14:01] root DEBUG: dt_boxes num : 3, elapse : 0.03070545196533203\n",
            "[2021/08/20 09:14:01] root DEBUG: rec_res num  : 3, elapse : 0.025460481643676758\n",
            "[[[77.0, 121.0], [268.0, 124.0], [268.0, 164.0], [77.0, 161.0]], ['EPPING', 0.9977332]]\n",
            "[[[100.0, 182.0], [247.0, 185.0], [246.0, 206.0], [100.0, 203.0]], ['Twinned with', 0.99726105]]\n",
            "[[[67.0, 204.0], [275.0, 209.0], [275.0, 231.0], [66.0, 227.0]], ['EppingenGermany', 0.98970574]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": false,
        "id": "czZapFYM3Qz-"
      },
      "source": [
        "## **韩文识别**\n",
        "![](https://ai-studio-static-online.cdn.bcebos.com/d4f9dd815d024114bd4eee7dca03f7222c135073b01642f3866684b404b75d2a)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "m1ZiZEi13Qz-",
        "outputId": "a5f2d2c8-2a02-4863-e4f2-a35425956e43"
      },
      "source": [
        "from agentocr import OCRSystem\n",
        "\n",
        "# 通过 config 参数来进行模型配置，内置多国语言的配置文件\n",
        "ocr = OCRSystem(config='korean')\n",
        "\n",
        "# 设置测试图片路径\n",
        "img_path = 'images/korean.jpg'\n",
        "\n",
        "# 调用 OCR API 进行全流程识别\n",
        "result = ocr.ocr(img_path)\n",
        "\n",
        "# 打印结果\n",
        "for line in result:\n",
        "    print(line)"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[2021/08/20 09:14:07] root INFO: All available providers: ['CPU']\n",
            "[2021/08/20 09:14:07] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:14:07] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:14:07] root INFO: Using providers: ['CPU']\n",
            "[2021/08/20 09:14:07] root DEBUG: dt_boxes num : 5, elapse : 0.2565634250640869\n",
            "[2021/08/20 09:14:08] root DEBUG: rec_res num  : 5, elapse : 0.05133700370788574\n",
            "[[[400.0, 518.0], [502.0, 513.0], [504.0, 550.0], [402.0, 555.0]], ['오야야', 0.7580958]]\n",
            "[[[156.0, 576.0], [760.0, 559.0], [761.0, 594.0], [157.0, 610.0]], ['내가간차좋아하는사양이성서서호과공증', 0.60792416]]\n",
            "[[[203.0, 636.0], [718.0, 625.0], [718.0, 662.0], [203.0, 672.0]], ['않다가죽어어킬것만갈하서어기를한다', 0.73020154]]\n",
            "[[[178.0, 691.0], [709.0, 683.0], [709.0, 717.0], [178.0, 724.0]], ['눈앞에아른아른거리는잘생긴열굴가푸', 0.6773215]]\n",
            "[[[225.0, 739.0], [658.0, 738.0], [658.0, 770.0], [225.0, 771.0]], ['귀에명도는 그의촉촉한 옥소리예', 0.83734596]]\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}
